package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.ImagesMapper;
import com.example.blog.pojo.Images;
import com.example.blog.pojo.Result;
import com.example.blog.service.ImagesServices;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ImagesServicesImpl extends ServiceImpl<ImagesMapper, Images> implements ImagesServices {
    @Resource
    private ImagesMapper imagesMapper;

    /***
     * 用户上传图片
     * @param images 上传用户和图片
     * @param token
     * @return 上传结果
     */
    @Override
    public Result<T> saveImages(Images images, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        images.setName(userName);
        int insert = imagesMapper.insert(images);
        if (insert > 0) {
            return Result.success("上传成功！");
        }
        return Result.failed("上传失败！");
    }

    /***
     * 展示用户上传图片
     * @param token
     * @return
     */
    @Override
    public List<Images> getUserImages(String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        QueryWrapper<Images> wrapper = new QueryWrapper<>();
        wrapper.eq("name", userName);
        return imagesMapper.selectList(wrapper);
    }

    /***
     * 根据id删除上传图片
     * @param id
     * @param token
     * @return 删除结果
     */
    @Override
    public Result<T> deleteImagesRecord(int id, String token) {
        int delete = imagesMapper.deleteById(id);
        if (delete > 0) {
            return Result.success("删除成功！");
        }
        return Result.failed("删除失败！");
    }
}
